package com.ruoyi.system.interceptor;

import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.Interceptor;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Forest.grm.AuthGRM;
import com.ruoyi.system.domain.Forest.grm.GRMData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;

@Component

public class GrmLoadDataInterceptor implements Interceptor<Object> {

    @Autowired
    private RedisCache redisCache;
    @Override
    public boolean beforeExecute(ForestRequest request) {
        String url = redisCache.getCacheObject(CacheConstants.GRM_TOKEN_URL);
        if (StringUtils.isNotEmpty(url)){
            request.setUrl(url);
            return true;
        }
        return false;
    }

    @Override
    public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
        String dataStr = response.getContent();
        //使用换行符分隔
        String[] split = dataStr.split("\r\n");
        GRMData grmData = new GRMData();
        if (split.length == 11){
            if (split[0].equals("ERROR")){
                grmData.setCode(1);
                grmData.setHighIn(0.0);
                grmData.setHighOut(0.0);
                grmData.setLowIn(0.0);
                grmData.setLowOut(0.0);
                grmData.setPower(0.0);
                grmData.setTemperature(0.0);
                grmData.setHeat(0.0);
                grmData.setPowerConsumption(0.0);
                grmData.setWaterTemperature(0.0);

            } else {
                grmData.setCode(0);
                if (split[2].equals("#ERROR#7")){
                    grmData.setHighIn(0.0);
                }else {
                    grmData.setHighIn(new BigDecimal(split[2]).setScale(1, RoundingMode.HALF_UP).doubleValue());
                }
                if (split[3].equals("#ERROR#7")){
                    grmData.setHighOut(0.0);
                } else {
                    grmData.setHighOut(new BigDecimal(split[3]).setScale(1, RoundingMode.HALF_UP).doubleValue());
                }
                if (split[4].equals("#ERROR#7")){
                    grmData.setLowIn(0.0);
                } else {
                    grmData.setLowIn(new BigDecimal(split[4]).setScale(1, RoundingMode.HALF_UP).doubleValue());
                }
                if (split[5].equals("#ERROR#7")){
                    grmData.setLowOut(0.0);
                } else {
                    grmData.setLowOut(new BigDecimal(split[5]).setScale(1, RoundingMode.HALF_UP).doubleValue());
                }

                if (split[6].equals("#ERROR#7")){
                    grmData.setPower(0.0);
                } else {
                    grmData.setPower(Double.valueOf(split[6]));
                }

                if (split[7].equals("#ERROR#7")){
                    grmData.setTemperature(0.0);
                } else {
                    grmData.setTemperature(Double.valueOf(split[7]));
                }

                if (split[8].equals("#ERROR#7")) {
                    grmData.setHeat(0.0);
                } else {
                    grmData.setHeat(Double.valueOf(split[8]));
                }

                if (split[9].equals("#ERROR#7")) {
                    grmData.setPowerConsumption(0.0);
                } else {
                    grmData.setPowerConsumption(Double.valueOf(split[9]));
                }
                if (split[10].equals("#ERROR#7")){
                    grmData.setWaterTemperature(0.0);
                } else {
                    grmData.setWaterTemperature(Double.valueOf(split[10]));
                }

            }
        }else {
            grmData.setCode(1);
            grmData.setHighIn(0.0);
            grmData.setHighOut(0.0);
            grmData.setLowIn(0.0);
            grmData.setLowOut(0.0);
            grmData.setPower(0.0);
            grmData.setTemperature(0.0);
            grmData.setHeat(0.0);
            grmData.setPowerConsumption(0.0);
            grmData.setWaterTemperature(0.0);
        }
        response.setResult(grmData);

        Interceptor.super.onSuccess(data, request, response);
    }
}